package com.souku.finance.application.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.CurrentBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.ui.invite.TenderDetailsActivity;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.CircleViewHomeNopoint;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/20.
 */

public class CurrentActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected CircleViewHomeNopoint circle;
    protected TextView tvCircle;
    protected RelativeLayout rlCurrentrecordlist;
    protected Button btnCurrentout;
    protected Button btnCurrentin;
    protected TextView tvCurrentRate;
    protected TextView tvRiseMinMoney;
    protected TextView tvRiseDuration;
    protected TextView tvRiseRate;
    protected TextView tvMoney;
    protected TextView tvCumulatedInterest;
    protected TextView tvAvailableInterest;
    protected TextView tvMaxInvestment;
    protected RelativeLayout rlCurrentmoneylist;
    protected ImageView topMore;
    private CurrentBean bean;

    @Override
    public void internet() {
        queryUserDealInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        queryUserDealInfo();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_current);
        initView();
        circle.setStoke(this);
        topTitle.setText("活期产品");
        topMore.setVisibility(View.VISIBLE);
        topMore.setImageResource(R.drawable.current_icon);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.rl_currentrecordlist) {
            if (bean == null) {
                queryUserDealInfo();
                return;
            }
            Intent intent = new Intent(this, CurrentDetailedActivity.class);
            intent.putExtra(Constant.Default, bean);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_currentout) {
            if (bean == null) {
                queryUserDealInfo();
                return;
            }
            Intent intent = new Intent(this, CurrentOutActivity.class);
            intent.putExtra(Constant.Default, bean);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_currentin) {
            Intent intent = new Intent(this, TenderDetailsActivity.class);
            intent.putExtra(Constant.Default, "1");
            startActivity(intent);
        } else if (view.getId() == R.id.rl_currentmoneylist) {
            Intent intent = new Intent(this, CurrentMoneyActivity.class);
            intent.putExtra(Constant.Default, "1");
            startActivity(intent);
        } else if (view.getId() == R.id.top_more) {
            ToastUtil.show("敬请期待");
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(CurrentActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        circle = (CircleViewHomeNopoint) findViewById(R.id.circle);
        tvCircle = (TextView) findViewById(R.id.tv_circle);
        rlCurrentrecordlist = (RelativeLayout) findViewById(R.id.rl_currentrecordlist);
        rlCurrentrecordlist.setOnClickListener(CurrentActivity.this);
        btnCurrentout = (Button) findViewById(R.id.btn_currentout);
        btnCurrentout.setOnClickListener(CurrentActivity.this);
        btnCurrentin = (Button) findViewById(R.id.btn_currentin);
        btnCurrentin.setOnClickListener(CurrentActivity.this);
        tvCurrentRate = (TextView) findViewById(R.id.tv_currentRate);
        tvRiseMinMoney = (TextView) findViewById(R.id.tv_riseMinMoney);
        tvRiseDuration = (TextView) findViewById(R.id.tv_riseDuration);
        tvRiseRate = (TextView) findViewById(R.id.tv_riseRate);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        tvCumulatedInterest = (TextView) findViewById(R.id.tv_cumulatedInterest);
        tvAvailableInterest = (TextView) findViewById(R.id.tv_availableInterest);
        tvMaxInvestment = (TextView) findViewById(R.id.tv_maxInvestment);
        rlCurrentmoneylist = (RelativeLayout) findViewById(R.id.rl_currentmoneylist);
        rlCurrentmoneylist.setOnClickListener(CurrentActivity.this);
        topMore = (ImageView) findViewById(R.id.top_more);
        topMore.setOnClickListener(this);
    }

    //获取活期详情
    private void queryUserDealInfo() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.QUERY_USER_DEAL_INFO, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), CurrentBean.class);
                    setData(bean);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void setData(CurrentBean bean) {
        double yesInterest = Double.parseDouble(MyTextUtil.setMoneyToElement(bean.getYesInterest()));
        circle.setPrograss(yesInterest, yesInterest, tvCircle);
        tvCurrentRate.setText(bean.getCurrentDealLoadDO().getCurrentRate() + "%");//年化率
        tvRiseMinMoney.setText(MyTextUtil.setMoneyToElement(bean.getRiseMinMoney()));
        tvRiseDuration.setText(bean.getRiseDuration());
        tvRiseRate.setText(bean.getRiseRate() + "%");
        tvMoney.setText(MyTextUtil.setMoneyToElement(bean.getCurrentDealLoadDO().getMoney()));//再投本金
        tvCumulatedInterest.setText(MyTextUtil.setMoneyToElement(bean.getCurrentDealLoadDO().getCumulatedInterest()));//累计收益
        tvAvailableInterest.setText(MyTextUtil.setMoneyToElement(bean.getCurrentDealLoadDO().getAvailableInterest()));//可提取收益(元)
        tvMaxInvestment.setText(MyTextUtil.setMoneyToElement(bean.getMaxInvestment()));
    }
}
